/**
 * Created by IntelliJ IDEA.
 * User: Slejus
 * Date: 13.9.17
 * Time: 11.51
 * To change this template use File | Settings | File Templates.
 */
window.location.assign("http://www.w3schools.com")

loadingMessage = Alfresco.util.PopupManager.displayMessage(
{
    displayTime: 0,
    text: '123',
    noEscape: true
});
loadingMessage.destroy();


//?date("EEE MMM dd HH:mm:ss zzz yyyy")?iso_utc

function getFormData() {
    var form = null;
    var formId = $("form[method=post]").attr('id');
    if (formId){
        form = Dom.get(formId);
    }
    if (form !== null)
    {
        var formData = {},
            length = form.elements.length;

        for (var i = 0; i < length; i++)
        {
            var element = form.elements[i],
                name = element.name;

            if (name == "-" || element.disabled || element.type === "button")
            {
                continue;
            }
            if (name == undefined || name == "")
            {
                name = element.id;
            }
            var value = (element.type === "textarea") ? element.value : YAHOO.lang.trim(element.value);
            if (name)
            {
                // check whether the input element is an array value
                if ((name.length > 2) && (name.substring(name.length - 2) == '[]'))
                {
                    name = name.substring(0, name.length - 2);
                    if (formData[name] === undefined)
                    {
                        formData[name] = new Array();
                    }
                    formData[name].push(value);
                }
                // check whether the input element is an object literal value
                else if (name.indexOf(".") > 0)
                {
                    var names = name.split(".");
                    var obj = formData;
                    var index;
                    for (var j = 0, k = names.length - 1; j < k; j++)
                    {
                        index = names[j];
                        if (obj[index] === undefined)
                        {
                            obj[index] = {};
                        }
                        obj = obj[index];
                    }
                    obj[names[j]] = value;
                }
                else if (!((element.type === "checkbox" || element.type === "radio") && !element.checked))
                {
                    if (element.type == "select-multiple")
                    {
                        for (var j = 0, jj = element.options.length; j < jj; j++)
                        {
                            if (element.options[j].selected)
                            {
                                if (formData[name] == undefined)
                                {
                                    formData[name] = new Array();
                                }
                                formData[name].push(element.options[j].value);
                            }
                        }
                    }
                    else
                    {
                        formData[name] = value;
                    }
                }
            }
        }

        return formData;
    }
}

var jsonData = getFormData();

var onDocumentPreviewLoaded = function(res) {
    var htmlPreview = res.serverResponse.responseText;
    var formEl = Dom.get("dynamicFormData");
    formEl.innerHTML= "";
    formEl.innerHTML = htmlPreview;
}
//Document preview
Alfresco.util.Ajax.jsonRequest(
{
    url: "/alfresco/service/components/workflowdocpreview",
    method: "POST",
    dataObj: jsonData,
    successCallback:
    {
        fn: onDocumentPreviewLoaded,
        scope: this
     },
    failureMessage:Alfresco.util.message("message.failure"),
    scope: this,
    execScripts: true
 });


Alfresco.util.Ajax.request(
    {
        url: Alfresco.constants.URL_SERVICECONTEXT + "components/form",
        dataObj:
        {
            htmlid: "template_x002e_start-workflow_x002e_start-workflow_x0023_default" + "-startWorkflowForm-" + Alfresco.util.generateDomId(),
            itemKind: "workflow",
            itemId: "activiti$perAppealDiv",
            mode: "create",
            submitType: "json",
            showCaption: true,
            formUI: true,
            showCancelButton: true,
            destination: ""
        },
        successCallback:
        {
            fn: StartWorkflow_onWorkflowFormLoaded,
            scope: this
        },
        failureMessage: console.log("message.failure"),
        scope: this,
        execScripts: true
    });

function StartWorkflow_onWorkflowFormLoaded(response)
{
    var formEl = Dom.get("template_x002e_start-workflow_x002e_start-workflow_x0023_default-workflowFormContainer");
    Dom.addClass(formEl, "hidden");
    formEl.innerHTML = response.serverResponse.responseText;
}

// Update Date Picker
new Alfresco.RcDatePicker("template_x002e_start-workflow_x002e_start-workflow_x0023_default-startWorkflowForm-alf-id3_prop_rccwf_perAppealData_iki-cntrl", "template_x002e_start-workflow_x002e_start-workflow_x0023_default-startWorkflowForm-alf-id3_prop_rccwf_perAppealData_iki").setOptions(
    {
        currentValue: "2013-09-25",
        minDate: "today",
        mandatory: "true"
    }).setMessages(
    "Message date test"
);